package com.merry.mq.springBoot.controller;

import com.merry.mq.springBoot.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author frx
 * @version 1.0
 * @date 2022/7/25  23:00
 * desc:发送延迟消息
 */
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    //开始发消息
    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable("message") String message){
        System.out.println("当前时间:"+new Date().toString()+",发送一条信息给两个TTL队列"+message);
        rabbitTemplate.convertAndSend("X","XA","消息来自ttl为10s的队列:"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自ttl为40s的队列:"+message);

    }

    @GetMapping("/sendMsg/delay/{message}/{delayTime}")
    public void sendDelayMsg(@PathVariable("message") String message
    ,@PathVariable("delayTime") int delayTime){
        System.out.println("当前时间:"+new Date().toString()+",发送一条信息给两个TTL队列"+message);
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE_NAME,
                DelayedQueueConfig.DELAYED_ROUTING_KEY,"消息来自delay的队列:"+message,msg -> {
            //设置延迟的时间
            msg.getMessageProperties().setDelay(delayTime);
            return msg;}
        );
    }
}
